﻿using RpcHelper;
using RpcModel;
using SqlExecHelper;
using SqlExecHelper.SetColumn;
using Wedonek.RpcStore.Service.Helper;
using Wedonek.RpcStore.Service.Model;

namespace Wedonek.RpcStore.Service.DAL
{
    internal class IpBlackListDAL : SqlBasicClass
    {
        public IpBlackListDAL() : base("IpBlackList", "RpcExtendService")
        {

        }

        public IpBlackDatum[] Query(IpBlackQuery query, IBasicPage paging, out long count)
        {
            paging.InitOrderBy("Id", true);
            return base.Query<IpBlackDatum>(paging.OrderBy, paging.Index, paging.Size, out count, query.ToWhere());
        }
        public IpBlackDatum Get(int id)
        {
            return base.GetRow<int, IpBlackDatum>("Id", id);
        }
        public bool CheckIsExists(long rpcMerId, string sysType, long ip)
        {
            return base.CheckIsExists(new ISqlWhere[] {
                new SqlWhere("RpcMerId", System.Data.SqlDbType.BigInt){Value=rpcMerId},
                new SqlWhere("SystemType", System.Data.SqlDbType.VarChar,50){Value=sysType},
                new SqlWhere("Ip", System.Data.SqlDbType.BigInt){Value=ip},
                new SqlWhere("EndIp", System.Data.SqlDbType.BigInt){IsNull=true},
                new SqlWhere("IsDrop", System.Data.SqlDbType.Bit){Value=false}
            });
        }
        public bool CheckIsExists(long rpcMerId, string sysType, long ip, long endIp)
        {
            return base.CheckIsExists(new ISqlWhere[] {
                new SqlWhere("RpcMerId", System.Data.SqlDbType.BigInt){Value=rpcMerId},
                new SqlWhere("SystemType", System.Data.SqlDbType.VarChar,50){Value=sysType},
                new SqlWhere("Ip", System.Data.SqlDbType.BigInt){Value=ip},
                new SqlWhere("EndIp", System.Data.SqlDbType.BigInt){Value=endIp},
                 new SqlWhere("IsDrop", System.Data.SqlDbType.Bit){Value=false}
            });
        }
        public void SetRemark(int id, string remark)
        {
            if (!base.Update(new ISqlSetColumn[] {
                new SqlSetColumn("Remark", System.Data.SqlDbType.NVarChar,50){Value=remark}
            }, "Id", id))
            {
                throw new ErrorException("rpc.store.ipBlack.set.fail");
            }
        }
        public int Add(AddIpBlack add)
        {
            base.Insert(add, out int id);
            return id;
        }
        public int Set(IpBlackDatum black)
        {
            AddIpBlack add = new AddIpBlack
            {
                EndIp = black.EndIp,
                Ip = black.Ip,
                OverTime = black.OverTime,
                IsDrop = false,
                Remark = black.Remark,
                RpcMerId = black.RpcMerId,
                SystemType = black.SystemType
            };
            using (base.BeginTrans())
            {
                if (!base.Drop<int>("Id", black.Id))
                {
                    throw new ErrorException("rpc.store.ipBlack.drop.fail");
                }
                base.Insert(add, out int id);
                this.CommitTrans();
                return id;
            }
        }
        public void Drop(IpBlackDatum black)
        {
            AddIpBlack add = new AddIpBlack
            {
                EndIp = black.EndIp,
                Ip = black.Ip,
                IsDrop = true,
                RpcMerId = black.RpcMerId,
                SystemType = black.SystemType
            };
            using (base.BeginTrans())
            {
                if (!base.Drop<int>("Id", black.Id))
                {
                    throw new ErrorException("rpc.store.ipBlack.drop.fail");
                }
                base.Insert(add);
                this.CommitTrans();
            }
        }
    }
}
